package com.example.tenmins;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DelayAlarm extends Activity {

	TextView delaymin;
	Button up, down, more;
	Context context;
	int id;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
		        // 키잠금 해제하기
		        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
		        // 화면 켜기
		        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        
		super.onCreate(savedInstanceState);
		setContentView(R.layout.delay_time);

		delaymin = (TextView) findViewById(R.id.delaymin);
		up = (Button) findViewById(R.id.up);
		down = (Button) findViewById(R.id.down);
		more = (Button) findViewById(R.id.moresleep);

		up.setOnClickListener(upmin);
		down.setOnClickListener(downmin);
		more.setOnClickListener(moresleep);
		
		context = getApplicationContext();
		
		Intent data = getIntent();
		id = data.getIntExtra("id", 0);
		
		
		
		PopupActivity.at_d.add(this);
	}

	OnClickListener upmin = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			if (delaymin.getText().equals("59"))
				delaymin.setText("00");
			else {
				int min = Integer.parseInt(delaymin.getText().toString());
				min++;
				if(min < 10)
					delaymin.setText("0" + min);
				else delaymin.setText(String.valueOf(min));
			}

		}

	};

	OnClickListener downmin = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			if (delaymin.getText().equals("00"))
				delaymin.setText("59");
			else {
				int min = Integer.parseInt(delaymin.getText().toString());
				min--;
				if(min < 10)
					delaymin.setText("0"+min);
				else delaymin.setText(String.valueOf(min));
			}

		}

	};

	OnClickListener moresleep = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			
			GregorianCalendar currentCalendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+09:00"));
			GregorianCalendar gregorianCalendar = currentCalendar;
			
			Database db = new Database(context);
			db.open();

			int hh = db.getHour(id);
			int mm = db.getMin(id);
			String ampm = db.getAMPM(id);
			
			if(ampm.equals("am"))
				gregorianCalendar.set(Calendar.AM_PM, Calendar.AM);
			else
				gregorianCalendar.set(Calendar.AM_PM, Calendar.PM);
			
			gregorianCalendar.set(Calendar.SECOND, 0);
			
			
			int delaymm = Integer.parseInt(delaymin.getText().toString());

			gregorianCalendar.add(Calendar.MINUTE, delaymm);

			Intent intent = new Intent(DelayAlarm.this, PopupActivity.class);
			intent.putExtra("reqCode", id);

			PendingIntent pi = PendingIntent.getActivity(DelayAlarm.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

			AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			alarm.set(AlarmManager.RTC_WAKEUP, gregorianCalendar.getTimeInMillis(), pi);

			for(int i = 0; i < PopupActivity.at_d.size(); i++)
				PopupActivity.at_d.get(i).finish();
		}

	};
}
